
package reports;

import coreModel.Item;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import java.text.DateFormat;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;



/** Generates a printable PDF document which consists of the Item barcode 
 * labels for a batch of items being added to the core object model.
 * 
 * Generally after creating the PDFGenerator object, you will call 
 * addItem(...) several times to populate the document, then printToFile() 
 * to save to document, then clearItems() to begin a new document. 
 * 
 * Alternatively you can create a new PDFGenerator for each document you 
 * want to create.
 *
 */
public class PDFGenerator 
{
	private String fileName;
	
	private ArrayList<Item> items;
	
	

	/** Default constructor (no args)
	 */
	public PDFGenerator()
	{
		//empty
		this.items = new ArrayList<Item>();
		File labels = new File("labels");
		if(!labels.exists())
			labels.mkdir();
		fileName="labels/label-";
		
	}

//	/** Constructor with filename
//	 */
//	public PDFGenerator(String fileName)
//	{
//		this.fileName = fileName;
//		this.items = new ArrayList<Item>();
//		
//		
//	}
	
	/** Adds a barcode label to the document for the given item.
	 * 
	 * @param item An item whose data should be displayed in the PDF.
	 */
	public void addItem(Item item)
	{
		//TODO finish method
		
		items.add(item);
		
		
	}
	
	/** Writes the labels to the PDF document file specified in fileName.
	 * @throws DocumentException 
	 * @throws IOException 
	 */
	public void printToFile() throws DocumentException, IOException
	{
		//TODO finish method
		
		SimpleDateFormat dfEntry = new SimpleDateFormat("MM/dd/yy");

		
		Document document = new Document();
		document.setMargins(0,0, 36, 36);
		
		fileName=fileName+System.currentTimeMillis()+".pdf";
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
		
		document.open();
		
		PdfPTable table = new PdfPTable(6); 

				 
		 PdfContentByte cb = writer.getDirectContent();

		 		 
		 
		 BarcodeEAN codeEAN = new BarcodeEAN();
		
		

			codeEAN.setCodeType(Barcode.UPCA);
			codeEAN.setCode("453487657489");
				
		 Image temp = codeEAN.createImageWithBarcode(cb, null, null);
			
		 table.setTotalWidth(90*6);
		 table.setLockedWidth(true);
		
		  
		 Font font = new Font(FontFamily.HELVETICA,7);
		 
		 		
		for(int i=0;i<items.size();i++){
		
			codeEAN.setCodeType(Barcode.UPCA);
			codeEAN.setCode(items.get(i).getBarcode().getValue());
		    			
			
			
			String description = items.get(i).getProduct().getDescription();
			
			
			
			if(description.length()>25){
				description = description.substring(0, 25);
			}
			
			
			
			Phrase p1 = new Phrase(description,font);
			
			
			String entryandexp = dfEntry.format(items.get(i).getEntryDate());
			
			
			if(items.get(i).getExpirationDate()!=null){
			entryandexp += " exp " + dfEntry.format(items.get(i).getExpirationDate());
			}
			
			
			Phrase p2 = new Phrase(entryandexp,font);
			
			p2.setLeading(6);
			
			Image im = codeEAN.createImageWithBarcode(cb, null, null);
			 
			
			PdfPCell cell = new PdfPCell();
			cell.addElement(p1);
			cell.addElement(p2);
			
			cell.addElement(im);
			
			
			 
			 
			 //cell.setPaddingLeft(5);
			 cell.setPaddingRight(6);
			 //cell.setBorderWidth(10);
			 cell.setPaddingBottom(6);
			 			 
			cell.setBorder(0);
			 
			//cell.setPadding(20);
			
			table.addCell(cell);
			
		}
		
		
		int restofrow = 6-items.size()%6;
		
		for(int j=0;j<restofrow;j++){
			PdfPCell cell2 = new PdfPCell();
				
			cell2.setBorder(0);
			 
			table.addCell(cell2);
				
		}
		
				
         
         document.add(table);

		
		document.close();
		
		java.awt.Desktop.getDesktop().open(new File(fileName));
			
		
		
		
	}
	
	/** Resets the generator to begin a new document.
	 */
	public void clearItems()
	{
		//TODO finish method
		fileName = "";
		items = null;
	}


	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}
